package com.auth.service.impl;

import com.auth.custom.LoginUserInfoHelper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.auth.mapper.SysUserMapper;
import com.auth.model.system.SysUser;
import com.auth.service.SysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author tang
 * @since 2023-07-15
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    /**
     * 更新用户状态
     *
     * @author Tangjiachang
     * @date 2023/7/25 22:18
     * @param id:
	 * @param status:
     * @return void
     **/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateStatus(Long id, Integer status) {
        try {
            SysUser sysUser = this.getById(id);
            sysUser.setStatus(status);
            this.updateById(sysUser);
        } catch (Exception e) {
            e.printStackTrace();
            //手工回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    /**
     * 根据用户名获取用户基本信息
     *
     * @author Tangjiachang
     * @date 2023/7/25 22:18
     * @param username:
     * @return SysUser
     **/
    @Override
    public SysUser getUserByUserName(String username) {
        return baseMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username));
    }

    /**
     * 获取当前用户基本信息
     *
     * @author Tangjiachang
     * @date 2023/7/25 22:18
     * @param :
     * @return Map<String,Object>
     **/
    @Override
    public Map<String, Object> getCurrentUser() {
        SysUser sysUser = baseMapper.selectById(LoginUserInfoHelper.getUserId());
        return new HashMap<String, Object>(0) {{
            put("name", sysUser.getName());
            put("phone", sysUser.getPhone());
            put("deptName", sysUser.getDeptName());
            put("postName", sysUser.getPostName());
        }};
    }
}
